package com.pdh.front.controller;

import com.pdh.bean.Express;
import com.pdh.bean.Message;
import com.pdh.bean.User;
import com.pdh.mvc.ResponseBody;
import com.pdh.service.ExpressService;
import com.pdh.util.JSONUtil;
import com.pdh.util.LoginUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: 彭德华
 * @Date 2021-08-29 22:19
 */
public class ExpressController {
    @ResponseBody("/front/findExpressByUserPhone.do")
    public String findByUserPhone(HttpServletRequest req, HttpServletResponse resp){//显示要取的快递和已经取的快递
        //1.获取微信用户
        User frontUser = LoginUtil.getFrontUser(req.getSession());
        String userPhone = frontUser.getUserPhone();
        //2.通过号码获取快递信息
        Message msg = ExpressService.findByUserPhone(userPhone);
        String json = JSONUtil.toJSON(msg);
        return json;
    }


    @ResponseBody("/front/userExpress.do")
    public String userExpressList(HttpServletRequest req,HttpServletResponse resp) {
        String userPhone = req.getParameter("userPhone");
        //展示到用户眼中的快递没有以删除的即可 但是这里获取到的仅仅是待取件的快递，待优化
        Message msg = ExpressService.findByUserPhoneAndStatus(userPhone, 1);
        return JSONUtil.toJSON(msg);
    }

    /**
     * 于查询总懒人排行榜 包括总榜、年榜、月榜
     * @return
     */
    @ResponseBody("/front/lazyBoardInfo.do")
    public String lazyBoardInfo(HttpServletRequest req,HttpServletResponse resp) {
        //0.获取是否分页标志 0-count 1-year 2-month
        Integer no = Integer.valueOf(req.getParameter("no"));
        //1.获取是否分页标志
        boolean paging = Boolean.parseBoolean(req.getParameter("paging"));
        //2.获取查询数据的起始索引值(偏移量)
        int offset = Integer.parseInt(req.getParameter("offset"));
        //3.获取当前页要查询的数量，注意不是当前页码 计算机不需要页码
        int limit = Integer.parseInt(req.getParameter("limit"));
        //4.查询数据
        Message msg = ExpressService.lazyBoardInfo(no,paging,offset,limit);
        String json = JSONUtil.toJSON(msg.getData());
        return json;
    }

    @ResponseBody("/front/findExpressByNumber.do")//为什么这里和前端地址都写/express/findExpressByNumber.do会找不到?
    public String findExpressByNumber(HttpServletRequest req,HttpServletResponse resp){
        //1.接收参数
        String expressNumber = req.getParameter("expressNum");
        Message msg = ExpressService.findByNumber(expressNumber);
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/front/findExpressByCode.do")//为什么这里和前端地址都写/express/findExpressByNumber.do会找不到?
    public String findExpressByCode(HttpServletRequest req,HttpServletResponse resp){
        //1.接收参数
        String code = req.getParameter("code");
        Message msg = ExpressService.findByCode(code);
        System.out.println(msg.getData().toString());
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/front/addExpress.do")
    public String addExpress(HttpServletRequest req,HttpServletResponse resp){
        //1.接收参数 四个参数
        String company = req.getParameter("company");
        String number = req.getParameter("number");
        String username = req.getParameter("username");
        String userPhone = req.getParameter("userPhone");
        //2.传递接收到的四个数据 其中的userPhone未知，创建一个userUtil类获取对应的号码 遵循对扩展开发对修改关闭
        //String number, String userPhone, String userName, String company, String sysPhone
        Express express = new Express(number,userPhone,username,company, LoginUtil.getUserPhone(req.getSession()));
        Message msg = ExpressService.insert(express);
        if(msg.getStatus() == 0){//后端发送的问题就这两个 其他得到问题前端可以进行解决
            //录入成功

            //查询user表中有无该用户(userPhone) 无则插入该用户
            /*User user = UserService.findByUserPhone(userPhone);
            if(user == null){
                User newUser = new User(username, "", userPhone, "");
                UserService.insert(newUser);
            }*/
        }else {
            //录入失败 只有单号重复会导致录入失败
        }
        String json = JSONUtil.toJSON(msg);
        return json;//返回到客户端 其中的data为空
    }

    @ResponseBody("/front/updateExpressStatus.do")
    public String updateStatus(HttpServletRequest req,HttpServletResponse resp){
        //1.接收参数
        String code = req.getParameter("code");
        Integer status = Integer.valueOf(req.getParameter("status"));

        //2.执行操作
        Message msg = ExpressService.updateStatus(code,status);
        msg.setResult("取件成功");
        String json = JSONUtil.toJSON(msg);
        return json;
    }
}
